package directory_sync;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class DisplayMain extends JFrame
{
	private static final long serialVersionUID = 1L;
	private static final int FRAME_WIDTH = 1024, FRAME_HEIGTH = 768;
	private static final String[] COLUMNS = {"Name","Type","Size","Date modified"};
	
	//log4j implementation
	private static Logger logger = Logger.getLogger("syslog");
	//swing components
	private DefaultTableModel sourceTblModel, targetTblModel;
	private JTable sourceTable, targetTable;
	private JTextField sourceDirTxt, targetDirTxt;
	private JLabel statusLbl;
	private JButton selSourceBtu, selTargetBtu, syncBtu;
	private String sourceDir, targetDir;
	
	public DisplayMain()
	{	
		logger.setLevel(Level.TRACE);
		
		JFrame frame = new JFrame("Directory Sync");
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int xFramePos = (int)(dim.getWidth()-FRAME_WIDTH)/2;
		int yFramePos = (int)(dim.getHeight()-FRAME_HEIGTH)/2;
		frame.setSize(FRAME_WIDTH, FRAME_HEIGTH);
		frame.setLocation(xFramePos ,yFramePos);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.add(getMainPanel(), BorderLayout.NORTH);
		frame.add(getActionPanel(), BorderLayout.CENTER);
		frame.add(getStatusPanel(), BorderLayout.SOUTH);
		frame.setVisible(true);

		SyncSelector selector = new SyncSelector(this);
		ButtonListener listener = new ButtonListener(this, selector, logger);
		selSourceBtu.addActionListener(listener);
		selTargetBtu.addActionListener(listener);
		syncBtu.addActionListener(listener);
	}

	//main panel
	private JPanel getMainPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 1));
		String text = "<html><font size=\"7\">Directory Sync</font></html>";
		JLabel txtLbl = new JLabel(text, SwingConstants.CENTER);
		panel.add(txtLbl);
		JPanel syncPanel = new JPanel();
		syncPanel.setLayout(new FlowLayout());
		ImageIcon picNorm = new ImageIcon("./src/directory_sync/pics/main_norm.gif");
		ImageIcon picRoll = new ImageIcon("./src/directory_sync/pics/main_roll.gif");
		syncBtu = new JButton(picNorm);
		syncBtu.setRolloverIcon(picRoll);
		syncBtu.setBorder(BorderFactory.createEmptyBorder());
		syncBtu.setContentAreaFilled(false);
		syncBtu.setOpaque(true);
		syncPanel.add(syncBtu);
		panel.add(syncPanel);
		return panel;
	}

	//action panel
	private JPanel getActionPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(getControlPanel(), BorderLayout.NORTH);
		panel.add(getTablePanel(), BorderLayout.CENTER);
		panel.add(getProcessPanel(), BorderLayout.SOUTH);
		return panel;
	}
	
	//control panels
	private JPanel getControlPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 2));
		panel.add(getSourceBtuPanel());
		panel.add(getTargetBtuPanel());
		panel.add(getSourceTxtPanel());
		panel.add(getTargetTxtPanel());
		return panel;
	}
	
	private JPanel getSourceBtuPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		selSourceBtu = new JButton("Select source folder");
		panel.add(selSourceBtu);
		return panel;
	}
	
	private JPanel getTargetBtuPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		selTargetBtu = new JButton("Select target folder");
		panel.add(selTargetBtu);
		return panel;
	}
	
	private JPanel getSourceTxtPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		sourceDirTxt = new JTextField();
		sourceDirTxt.setPreferredSize(new Dimension(452, 20));
		sourceDirTxt.setEditable(false);
		setDirectoryLabel("",0);
		panel.add(sourceDirTxt);
		return panel;
	}
	
	private JPanel getTargetTxtPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		targetDirTxt = new JTextField();
		targetDirTxt.setPreferredSize(new Dimension(452, 20));
		targetDirTxt.setEditable(false);
		setDirectoryLabel("",1);
		panel.add(targetDirTxt);
		return panel;
	}

	//table panels
	private JPanel getTablePanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 2));
		panel.add(getSourcePanel());
		panel.add(getTargetPanel());
		return panel;
	}
	
	private Component getSourcePanel()
	{
		JPanel panel = new JPanel();
		Object[][] data = null;
		sourceTblModel = new DefaultTableModel(data, COLUMNS);
		sourceTable = new JTable(sourceTblModel)
		{
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int rowIndex, int colIndex)
			{
				return false;
			}
		};
		TableColumn column = null;
		for (int i = 0; i < 3; i++) {
			column = sourceTable.getColumnModel().getColumn(i);
	        if(i == 0)
	        {
	            column.setPreferredWidth(165);
	        } 
	        else if(i == 1 || i==2)
	        {
	            column.setPreferredWidth(25);
	        }
	        else
	        {
	            column.setPreferredWidth(50);
	        }
	    }    
	    
		JScrollPane scrollPane = new JScrollPane(sourceTable);
		panel.add(scrollPane);
		return panel;
	}
	
	private Component getTargetPanel()
	{
		JPanel panel = new JPanel();
		Object[][] data = null;
		targetTblModel = new DefaultTableModel(data, COLUMNS);
		targetTable = new JTable(targetTblModel)
		{
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int rowIndex, int colIndex)
			{
				return false;
			}
		};
		TableColumn column = null;
		for (int i = 0; i < 3; i++) {
			column = targetTable.getColumnModel().getColumn(i);
	        if(i == 0)
	        {
	            column.setPreferredWidth(165);
	        } 
	        else if(i == 1 || i==2)
	        {
	            column.setPreferredWidth(25);
	        }
	        else
	        {
	            column.setPreferredWidth(50);
	        }
	    }    
	    
		JScrollPane scrollPane = new JScrollPane(targetTable);
		panel.add(scrollPane);
		return panel;
	}

	//process panels
	private JPanel getProcessPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 2));
		
		return panel;
	}
	
	//status panel
	private JPanel getStatusPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 1));
		panel.setOpaque(true);
		panel.setBackground(Color.LIGHT_GRAY);
		statusLbl = new JLabel("Status: ", SwingConstants.LEFT);
		panel.add(statusLbl);
		return panel;
	}

	//setters
	protected void clearTable(int identifier)
	{
		if(identifier == 0)
		{
			while(sourceTblModel.getRowCount() > 0)
			{
				for(int i=0; i<sourceTblModel.getRowCount(); i++)
				{
					sourceTblModel.removeRow(i);
				}
			}
		}
		if(identifier == 1)
		{
			while(targetTblModel.getRowCount() > 0)
			{
				for(int i=0; i<targetTblModel.getRowCount(); i++)
				{
					targetTblModel.removeRow(i);
				}
			}
		}
	}
	
	protected void addRowToTable(Object[] data, int identifier)
	{
		if(identifier == 0)
		{
			sourceTblModel.addRow(data);
		}
		if(identifier == 1)
		{
			targetTblModel.addRow(data);
		}
	}
	
	protected void setDirectoryLabel(String text, int identifier)
	{
		if(identifier == 0)
		{
			String initial = "FROM:  ";
			sourceDir = text;
			sourceDirTxt.setForeground(new Color(0, 5, 101));
			sourceDirTxt.setText(initial + text);
		}
		if(identifier == 1)
		{
			String initial = "TO:  ";
			targetDir = text;
			targetDirTxt.setForeground(new Color(0, 5, 101));
			targetDirTxt.setText(initial + text);
		}
	}
	
	protected void setStatusLabel(String text, Color color)
	{
		String initial = "Status:  ";
		statusLbl.setText(initial + text);
		statusLbl.setForeground(color);
	}

	//getters
	protected boolean tableNotEmpty(int identifier)
	{
		if(identifier == 0)
		{
			while(sourceTblModel.getRowCount() > 0)
			{
				return true;
			}
		}
		if(identifier == 1)
		{
			while(targetTblModel.getRowCount() > 0)
			{
				return true;
			}
		}
		return false;
	}
	
	protected String getSourceDir()
	{
		return sourceDir;
	}
	
	protected String getTargetDir()
	{
		return targetDir;
	}
	
	protected JButton getSelSourceBtu()
	{
		return selSourceBtu;
	}
	
	protected JButton getSelTargetBtu()
	{
		return selTargetBtu;
	}

	protected JButton getSyncBtu()
	{
		return syncBtu;
	}
}